const mongoose = require('mongoose')
const Image = mongoose.model('Image')
const Comment = mongoose.model('Comment')


// const statsfn = async () => {
//   let imageStats = await Image.aggregate()
//   .group({
//       _id: null,
//       numOfImages: {$sum: 1},
//       numOfLikes: {$sum: "$likes"},
//       numOfVisits: {$sum: "$visit"}
//     })
//   console.log(imageStats)
//   const numOfComments = await Comment.countDocuments()

//   return{
//     ...imageStats[0],
//     numOfComments
    
//   }
// }

// 使用 Promise.all

const statsfn = () => Promise.all([
  Image.aggregate()
    .group({
      _id: null,
      numOfImages: {$sum: 1},
      numOfLikes: {$sum: "$likes"},
      numOfVisits: {$sum: "$visit"}
    }),
    Comment.countDocuments()
    
]).then(([obj1, numOfComments]) => ({
  ...obj1[0], numOfComments
}))

module.exports = statsfn